package com.ruoyi.tzai.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.tzai.domain.TTSEngineConfig;
import com.ruoyi.tzai.service.ITTSEngineConfigService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * TTS引擎配置Controller
 * 
 * @author tzai
 * @date 2025-10-27
 */
@Api(tags = "TTS引擎配置")
@RestController
@RequestMapping("/tzai/tts/engine")
public class TTSEngineConfigController extends BaseController
{
    @Autowired
    private ITTSEngineConfigService ttsEngineConfigService;

    @Autowired
    private com.ruoyi.tzai.service.tts.TTSServiceFactory ttsServiceFactory;

    /**
     * 查询TTS引擎配置列表
     */
    @ApiOperation("查询TTS引擎配置列表")
    @PreAuthorize("@ss.hasPermi('tzai:tts:engine:list')")
    @GetMapping("/list")
    public TableDataInfo list(TTSEngineConfig ttsEngineConfig)
    {
        startPage();
        List<TTSEngineConfig> list = ttsEngineConfigService.selectTTSEngineConfigList(ttsEngineConfig);
        return getDataTable(list);
    }

    /**
     * 查询支持时间戳的TTS引擎配置列表
     */
    @ApiOperation("查询支持时间戳的TTS引擎配置列表")
    @GetMapping("/list/withTimestamp")
    public AjaxResult listWithTimestamp()
    {
        List<TTSEngineConfig> list = ttsEngineConfigService.selectTTSEngineConfigWithTimestamp();
        return success(list);
    }

    /**
     * 导出TTS引擎配置列表
     */
    @ApiOperation("导出TTS引擎配置列表")
    @PreAuthorize("@ss.hasPermi('tzai:tts:engine:export')")
    @Log(title = "TTS引擎配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TTSEngineConfig ttsEngineConfig)
    {
        List<TTSEngineConfig> list = ttsEngineConfigService.selectTTSEngineConfigList(ttsEngineConfig);
        ExcelUtil<TTSEngineConfig> util = new ExcelUtil<TTSEngineConfig>(TTSEngineConfig.class);
        util.exportExcel(response, list, "TTS引擎配置数据");
    }

    /**
     * 获取TTS引擎配置详细信息
     */
    @ApiOperation("获取TTS引擎配置详细信息")
    @PreAuthorize("@ss.hasPermi('tzai:tts:engine:query')")
    @GetMapping(value = "/{engineId}")
    public AjaxResult getInfo(@PathVariable("engineId") Long engineId)
    {
        return success(ttsEngineConfigService.selectTTSEngineConfigByEngineId(engineId));
    }

    /**
     * 获取默认TTS引擎配置
     */
    @ApiOperation("获取默认TTS引擎配置")
    @GetMapping(value = "/default")
    public AjaxResult getDefault()
    {
        return success(ttsEngineConfigService.selectDefaultTTSEngineConfig());
    }

    /**
     * 根据引擎代码获取TTS引擎配置
     */
    @ApiOperation("根据引擎代码获取TTS引擎配置")
    @GetMapping(value = "/code/{engineCode}")
    public AjaxResult getInfoByCode(@PathVariable("engineCode") String engineCode)
    {
        return success(ttsEngineConfigService.selectTTSEngineConfigByEngineCode(engineCode));
    }

    /**
     * 新增TTS引擎配置
     */
    @ApiOperation("新增TTS引擎配置")
    @PreAuthorize("@ss.hasPermi('tzai:tts:engine:add')")
    @Log(title = "TTS引擎配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TTSEngineConfig ttsEngineConfig)
    {
        return toAjax(ttsEngineConfigService.insertTTSEngineConfig(ttsEngineConfig));
    }

    /**
     * 修改TTS引擎配置
     */
    @ApiOperation("修改TTS引擎配置")
    @PreAuthorize("@ss.hasPermi('tzai:tts:engine:edit')")
    @Log(title = "TTS引擎配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TTSEngineConfig ttsEngineConfig)
    {
        return toAjax(ttsEngineConfigService.updateTTSEngineConfig(ttsEngineConfig));
    }

    /**
     * 删除TTS引擎配置
     */
    @ApiOperation("删除TTS引擎配置")
    @PreAuthorize("@ss.hasPermi('tzai:tts:engine:remove')")
    @Log(title = "TTS引擎配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{engineIds}")
    public AjaxResult remove(@PathVariable Long[] engineIds)
    {
        return toAjax(ttsEngineConfigService.deleteTTSEngineConfigByEngineIds(engineIds));
    }

    /**
     * 设置为默认引擎
     */
    @ApiOperation("设置为默认引擎")
    @PreAuthorize("@ss.hasPermi('tzai:tts:engine:edit')")
    @Log(title = "TTS引擎配置", businessType = BusinessType.UPDATE)
    @PutMapping("/setDefault/{engineId}")
    public AjaxResult setDefault(@PathVariable Long engineId)
    {
        return toAjax(ttsEngineConfigService.setAsDefault(engineId));
    }

    /**
     * 测试引擎连接
     */
    @ApiOperation("测试TTS引擎连接")
    @PreAuthorize("@ss.hasPermi('tzai:tts:engine:test')")
    @PostMapping("/test/{engineId}")
    public AjaxResult testConnection(@PathVariable Long engineId)
    {
        boolean result = ttsEngineConfigService.testConnection(engineId);
        return result ? success("连接成功") : error("连接失败");
    }

    /**
     * 试听测试
     */
    @ApiOperation("TTS试听测试")
    @PreAuthorize("@ss.hasPermi('tzai:tts:engine:test')")
    @PostMapping("/testAudio/{engineId}")
    public AjaxResult testAudio(@PathVariable Long engineId, @RequestParam String text)
    {
        String audioUrl = ttsEngineConfigService.testAudio(engineId, text);
        if (audioUrl != null) {
            return success(audioUrl);
        }
        return error("测试失败");
    }
}

